/*
 */
package it.pride.primefacesShowcase.controller;

import it.pride.primefacesShowcase.model.User;
import it.pride.primefacesShowcase.service.UserService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 *
 * @author eluibon
 */
@Controller(value = "serverDataModel")
@Scope("session")
public class UserLazyDataModel extends LazyDataModel<User> {

    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Autowired
    UserService userService;

    private List<SelectItem> filteredCities;
    
    private User selectedUser ;

    public void init() {

        filteredCities = new ArrayList<SelectItem>();
        filteredCities.add(new SelectItem("", "-------------------"));
        for (String city : userService.findAllCities()) {
            filteredCities.add(new SelectItem(city, city));
        }        
    }

    public UserLazyDataModel() {
    }

    @Override
    public User getRowData(String rowKey) {

        return userService.findById(Long.valueOf(rowKey));
    }

    @Override
    public Object getRowKey(User user) {
        return user.getId();
    }

    @Override
    public List<User> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {

        this.setRowCount(userService.countForLazyDatatable(filters));
        return userService.findForLazyDatatable(first, pageSize, sortField, sortOrder, filters);
    }

    public List<SelectItem> getFilteredCities() {
        return filteredCities;
    }

    public void setFilteredCities(List<SelectItem> filteredCities) {
        this.filteredCities = filteredCities;
    }

    public User getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(User selectedUser) {
        this.selectedUser = selectedUser;
    }

    
}
